package cn.billy.serve.controller;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.utils.LoginContext;
import cn.billy.org.domain.Employee;
import cn.billy.org.service.IEmployeeService;
import cn.billy.serve.domain.ProductDetail;
import cn.billy.serve.service.IProductDetailService;
import cn.billy.serve.service.IProductService;
import cn.billy.serve.domain.Product;
import cn.billy.serve.query.ProductQuery;
import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import cn.billy.user.domain.Logininfo;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IProductDetailService productDetailService;
    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product, HttpServletRequest req){
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        product.setShopId(employee.getShopId());
        System.out.println("32432432432432"+product);
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.save(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product getById(@PathVariable("id")Long id)
    {
        return productService.getById(id);
    }


    //获取用户
    @GetMapping("/productDetail/{id}")
    public ProductDetail productDetail(@PathVariable("id")Long id)
    {
        System.out.println(productDetailService.getProductId(id));
        return  productDetailService.getProductId(id);
    }
    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> loadAll(){

        return productService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> queryData(@RequestBody ProductQuery query)
    {
        System.out.println("===================" + query.getState());
//        if (query.getState() == 0){
//
//            query.setState(null);
//        }
        return productService.queryData(query);
    }

    @PostMapping("/onsale")
    public AjaxResult onSalePet(@RequestBody List<Long> ids){
        try {
            productService.onSalePet(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSalePet(@RequestBody List<Long> ids){
        try {
            productService.offSalePet(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PostMapping("/dels")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            productService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
}
